package com.yinhaochuan.ir;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class retrieval {

	static Pattern p_content;
	static Pattern p_num;

	public static void GetKeyWord(Vector<String> keys, Vector<String> nums) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream("topic,txt")));
		StringBuffer str_buf = new StringBuffer();
		char char_buf[] = new char[1024];

		int i = 0;
		while ((i = in.read(char_buf, 0, 1024)) != -1) {
			str_buf.append(char_buf, 0, i);
		}
		in.close();
		GetAll(keys, p_content.matcher(str_buf.toString()));
		GetAll(nums, p_num.matcher(str_buf.toString()));

	}

	public static void GetAll(Vector<String> v, Matcher m) {
		int start = 0;

		while (m.find(start)) {
			String key = m.group(1);
			v.add(key);
			start = m.end();
		}
	}

	static Pattern p_hz = Pattern
			.compile("[\u3040-\u318f\u3300-\u337f\u3400-\u3d2d\u4e00-\u9fff\uf900-\ufaff]+");
	static Pattern p_en = Pattern.compile("[A-Za-z0-9]+");

	public static String createQuery(String text) {
		StringBuffer qb = new StringBuffer();
		int start = 0;
		Matcher matcher = p_hz.matcher(text);

		while (matcher.find(start)) {
			String str = matcher.group();
			for (int i = 0; i < str.length() - 1; i++) {
				qb.append(str.substring(i, i + 2) + " ");
			}

			start = matcher.end();
		}

		start = 0;
		matcher = p_en.matcher(text);

		while (matcher.find(start)) {
			qb.append(matcher.group() + " ");
			start = matcher.end();
		}
		System.out.println(qb.toString());

		return qb.toString();
	}

	public static void search(String tag, String out)
			throws Exception {
//		p_content = Pattern.compile("<" + tag + ">([\u0000-\uffff]*?)</" + tag
//				+ ">");
//
//		String index = "./data/index";
//		IndexReader reader = IndexReader
//				.open(FSDirectory.open(new File(index)));
////		IndexSearcher searcher = new IndexSearcher(reader);
////		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
//		
//		PrintWriter pw = new PrintWriter(new FileOutputStream(out));
		
		
	}

}
